//
//  SYWXClipViewController.m
//  demoSS
//
//  Created by hc on 2019/4/26.
//  Copyright © 2019 hc. All rights reserved.
//

#import "SYWXClipViewController.h"
#import "JPImageresizerView.h"
#import "SYBasicSDK.h"
#import "AFNetworking.h"
#import "Masonry.h"
#import "ReactiveObjC.h"
#import "FMDB.h"

@interface SYWXClipViewController ()

@property (nonatomic, strong) JPImageresizerView *imageresizerView;
@property (nonatomic, strong) JPImageresizerConfigure *configure;

@property (nonatomic, strong) UIButton *cancleBtn;
@property (nonatomic, strong) UIButton *sureBtn;
@property (nonatomic, strong) UIButton *originalBtn;
@property (nonatomic, strong) UIButton *rotateBtn;

@end

@implementation SYWXClipViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)setImage:(UIImage *)image {
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(50, 0, 100, 0);
    self.configure = [JPImageresizerConfigure defaultConfigureWithResizeImage:image make:^(JPImageresizerConfigure *configure) {
        configure.jp_contentInsets(contentInsets);
    }];
    
    __weak typeof(self) wSelf = self;
    self.imageresizerView = [JPImageresizerView imageresizerViewWithConfigure:self.configure imageresizerIsCanRecovery:^(BOOL isCanRecovery) {
        __strong typeof(wSelf) sSelf = wSelf;
        if (!sSelf) return;
        // 当不需要重置设置按钮不可点
    } imageresizerIsPrepareToScale:^(BOOL isPrepareToScale) {
        __strong typeof(wSelf) sSelf = wSelf;
        if (!sSelf) return;
        // 当预备缩放设置按钮不可点，结束后可点击
        //        BOOL enabled = !isPrepareToScale;
    }];
    
    self.imageresizerView.frameType = JPClassicFrameType;
    self.imageresizerView.resizeWHScale = 1;
    [self.view addSubview:self.imageresizerView];
    
    [self setupSubviews];
}

- (void)handleClipAction:(UIButton *)btn {
    __weak typeof(self) weakSelf = self;
    
    // 1.默认以imageView的宽度为参照宽度进行裁剪
    [self.imageresizerView originImageresizerWithComplete:^(UIImage *resizeImage) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        if (!strongSelf) return;
        
        if (!resizeImage) {
            NSLog(@"没有裁剪图片");
            return;
        }
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(clipViewController:Image:)]) {
            [self.delegate clipViewController:self Image:resizeImage];
        }
    }];
}

- (void)handleCancleAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(cancleClipViewController:)]) {
        [self.delegate cancleClipViewController:self];
    }
}

//  旋转
- (void)handleRotateAction:(UIButton *)button {
    [self.imageresizerView rotation];
}

//  重置
- (void)handleRecoveryAction:(UIButton *)button {
    [self.imageresizerView recoveryByCurrentResizeWHScale];
}

- (void)setupSubviews {
    NSInteger height = 0;
    if (SYKeyboardIS_iPhoneX) {
        height += 49;
    }
    
    [self.view addSubview:self.sureBtn];
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-height);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(50);
        make.right.equalTo(self.view);
    }];
    
    [self.view addSubview:self.cancleBtn];
    [self.cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-height);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(50);
        make.left.equalTo(self.view);
    }];
    
    [self.view addSubview:self.originalBtn];
    [self.originalBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-height);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(50);
        make.centerX.equalTo(self.view);
    }];
    
    [self.view addSubview:self.rotateBtn];
    [self.rotateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(self.cancleBtn);
        make.bottom.equalTo(self.cancleBtn.mas_top);
    }];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(1);
        make.top.equalTo(self.rotateBtn.mas_bottom);
    }];
}

#pragma mark - 懒加载
- (UIButton *)cancleBtn {
    if (!_cancleBtn) {
        _cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancleBtn addTarget:self action:@selector(handleCancleAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancleBtn;
}
- (UIButton *)sureBtn {
    if (!_sureBtn) {
        _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sureBtn setTitle:@"完成" forState:UIControlStateNormal];
        [_sureBtn addTarget:self action:@selector(handleClipAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}
- (UIButton *)originalBtn {
    if (!_originalBtn) {
        _originalBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_originalBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_originalBtn setTitle:@"还原" forState:UIControlStateNormal];
        [_originalBtn addTarget:self action:@selector(handleRecoveryAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _originalBtn;
}
- (UIButton *)rotateBtn {
    if (!_rotateBtn) {
        _rotateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rotateBtn setImage:[UIImage imageNamed:@"xuanzhuan"] forState:UIControlStateNormal];
        [_rotateBtn addTarget:self action:@selector(handleRotateAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rotateBtn;
}

@end
